using System;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;
using AddinExpress.OL;
using OL = Microsoft.Office.Interop.Outlook;

namespace uTILLIty.Outlook.FlexFileAddIn
{
	public partial class PaneIntegration : ADXOlForm
	{
		protected FilterableFolderListController Controller;

		public PaneIntegration()
		{
			if (components == null)
				components = new Container();

			InitializeComponent();
			FlexFile.MapiFolderManager.LoadStatusChanged += OnFolderLoadStatusChanged;
			Load += (o, e) => InitializeController();
		}

		private delegate void ProcessMessage(string msg);
		private void OnFolderLoadStatusChanged(object sender, EventArgs<string> e)
		{
			if (InvokeRequired)
			{
				Invoke(new ProcessMessage(InternalSetStatusText), e.Value);
				return;
			}
			InternalSetStatusText(e.Value);
		}

		private void InternalSetStatusText(string msg)
		{
			textBox1.AppendText(string.Format("{0}: {1}\r\n", DateTime.Now.ToLongTimeString(), msg ?? ""));
		}

		private void InitializeController()
		{
			if (FlexFile.MapiFolderManager.FolderList.SynchronizationContext == null)
				FlexFile.MapiFolderManager.FolderList.SynchronizationContext = SynchronizationContext.Current;
			Controller = new FilterableFolderListController(filterableFolderList1, FlexFile.MapiFolderManager,
			                                                (OL.Application)OutlookAppObj);
			components.Add(Controller);
		}

		private void OnClearTextboxButtonClicked(object sender, System.EventArgs e)
		{
			textBox1.Clear();
		}

		private void OnWordWrapButtonClicked(object sender, EventArgs e)
		{
			WordWrapButton.Checked = !WordWrapButton.Checked;
			textBox1.WordWrap = WordWrapButton.Checked;
			textBox1.ScrollBars = WordWrapButton.Checked ? ScrollBars.Vertical : ScrollBars.Both;
		}

		private void OnReloadFolderListButtonClicked(object sender, EventArgs e)
		{
			Controller.MapiFolderManager.BeginLoadingFolders();
		}

	}
}

